﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace BookOnline.Admin.Include
{
    public partial class Category : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                View_data();
            }
        }

        private string getjQueryCode(string jsCodetoRun)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("$(document).ready(function() {");
            sb.AppendLine(jsCodetoRun);
            sb.AppendLine(" });");

            return sb.ToString();
        }

        private void runjQueryCode(string jsCodetoRun)
        {
            ScriptManager requestSM = ScriptManager.GetCurrent(this);
            if (requestSM != null && requestSM.IsInAsyncPostBack)
            {
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), getjQueryCode(jsCodetoRun), true);
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(typeof(Page), Guid.NewGuid().ToString(), getjQueryCode(jsCodetoRun), true);
            }
        }

        private void View_data()
        {
            GridView1.DataSource = Business.CatService.getAll();
            GridView1.DataBind();
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            List<Data.Cat> list = Business.CatService.getByID(e.CommandArgument.ToString());
            if (e.CommandName == "sua")
            {
                txtCatName.Text = list[0].CatName;
                txtCatParent.Text = list[0].CatParent;
                txtCatID.Text = list[0].CatID;
                btnSave.Text = "Save";
                btnCancel.Visible = true;
            }
            if (e.CommandName=="xoa")
            {
                Business.CatService.Cat_Delete(e.CommandArgument.ToString());
                View_data();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (txtCatName.Text==null||txtCatName.Text=="")
            {
                txtCatName.Focus();
            }
            else if (txtCatParent.Text == null || txtCatParent.Text == "")
            {
                txtCatParent.Focus();
            }
            else { 
                Data.Cat data = new Data.Cat();
                data.CatName=txtCatName.Text;
                data.CatParent=txtCatParent.Text;
                data.CatID = txtCatID.Text;
                if (btnSave.Text=="Add")
                {
                    
                    bool kq = Business.CatService.Cat_Add(data);
                    if (kq)
                    {
                        txtCatName.Text = "";
                        txtCatParent.Text = "";
                        View_data();
                    }
                    else
                    {
                       Alert.Show("fail");
                    }
                }
                if (btnSave.Text=="Save")
                {
                    bool kq = Business.CatService.Cat_Update(data);
                    if (kq)
                    {
                        txtCatName.Text = "";
                        txtCatParent.Text = "";
                        txtCatID.Text = "";
                        btnCancel.Visible = false;
                        btnSave.Text = "Add";
                        View_data();
                    }
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            txtCatName.Text = "";
            txtCatParent.Text = "";
            txtCatID.Text = "";
            btnCancel.Visible = false;
            btnSave.Text = "Add";
        }
    }
}